package arkham.data;

import java.util.*;
import arkham.data.Globals.MythosMovement;
import arkham.data.Globals.MythosType;
import arkham.jaxb.encounter.*;
import arkham.util.AHLogger;
import arkham.util.Pair;

public class MythosCard implements NamedEntity
{

	private String name;
	private String gateLocation;
	private List<String> clueLocation;
	private String description;
	private MythosType type;
	private MythosMovement movement;
	private String special;
	private List<Pair<String, String>> effects;

	public MythosCard(String name)
	{
		this.name = name;
		clueLocation = new ArrayList<String>();
		effects = new ArrayList<Pair<String, String>>();
	}

	/**
	 * Gets the name of the mythos card
	 * @return the name of the mythos card
	 */
	public String getName() {
		return name;
	}

	/**
	 * Gets the gate location on the mythos card
	 * @return the gate location, or empty string if no gate opened
	 */
	public String getGateLocation() {
		return gateLocation;
	}

	/**
	 * Gets the clue location(s) on the mythos card
	 * @return the places which clues appear
	 */
	public List<String> getClueLocation() {
		return clueLocation;
	}

	/**
	 * Gets the text on the mythos card
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * Gets the type of mythos card
	 * @return the type
	 */
	public MythosType getType() {
		return type;
	}

	/**
	 * Gets the movement pattern of monsters on the mythos card
	 * @return the movement
	 */
	public MythosMovement getMovement() {
		return movement;
	}

	/**
	 * Gets a list of names of mythos effects to put into play and the
	 * kind of effect they are, in pairs, eg. ("Headline", "Feds Raid Arkham!")
	 * @return the effects
	 */
	public List<Pair<String, String>> getEffects() {
		return effects;
	}

	/**
	 * Gets the special effects of this mythos card. Examples are:
	 * Gate Burst, Next Act Begins, Reshuffle Deck
	 * @return any special effects present, or empty string if none
	 */
	public String getSpecial() {
		return special;
	}

	@Override
	public String toString()
	{
		return name;
	}

	/**
	 * Creates the data representation of the mythos card from the XML info
	 * and performs a stringent validation check to make sure the XML data is
	 * valid.
	 * <br>Will return <b>null</b> on a failure.
	 *
	 * @param xmlmyth the XML mythos card info
	 * @return the data representation of the mythos card
	 */
	public static MythosCard createMythosFromXml(MythosCardType xmlmyth)
	{
		MythosCard mycard = new MythosCard(xmlmyth.getName());
		mycard.description = xmlmyth.getDescription().replace("\\n", "\n");
		if (xmlmyth.getGateLocation() != null)
		{
			mycard.gateLocation = xmlmyth.getGateLocation();
		}
		mycard.clueLocation.addAll(xmlmyth.getClueLocation());
		if (!MythosType.isValidMythosType(xmlmyth.getType()))
		{
			AHLogger.error(mycard.name + " has invalid mythos card type: " + xmlmyth.getType());
			return null;
		}
		mycard.type = MythosType.getMythosType(xmlmyth.getType());
		if (!MythosMovement.isValidMythosMovement(xmlmyth.getMovement()))
		{
			AHLogger.error(mycard.name + " has invalid mythos movement: " + xmlmyth.getMovement());
			return null;
		}
		mycard.movement = MythosMovement.getMythosMovement(xmlmyth.getMovement());
		mycard.special = xmlmyth.getSpecial();

		for (String s : xmlmyth.getEnvironment())
		{
			mycard.effects.add(new Pair<String, String>("Environment", s));
		}
		for (String s : xmlmyth.getHeadline())
		{
			mycard.effects.add(new Pair<String, String>("Headline", s));
		}
		for (String s : xmlmyth.getLocalEvent())
		{
			mycard.effects.add(new Pair<String, String>("LocalEvent", s));
		}
		for (String s : xmlmyth.getRumor())
		{
			mycard.effects.add(new Pair<String, String>("Rumor", s));
		}

		return mycard;
	}

}
